#include<iostream>
#include<stdio.h>
#include<queue>
#include<vector>
using namespace std;

long long TotalCost = 0;
long long LeafCost = 0;


class Node
{
public:
		long long weight;
		Node *left;
		Node *right;
		Node(long long w):weight(w),left(NULL),right(NULL) {}
		Node(long long w , Node* l , Node* r):weight(w),left(l),right(r) {}
};

void InOrder(Node *p)
{
	if(p!=NULL)
	{
		InOrder(p->left);
		TotalCost += p->weight;
		InOrder(p->right);
	}
}

void freeTree(Node *p)
{
	if(p->left!=NULL)
		freeTree(p->left);
	if(p->right!=NULL)
		freeTree(p->right);
	delete(p);
}

class cmp
{
public:
		bool operator()(Node* a , Node* b)
		{
			return a->weight >= b->weight;
		}
};

int main()
{
	int n;
	int x;
	priority_queue<Node* , vector<Node*>,cmp >  q;
	TotalCost = 0;
	LeafCost = 0;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>x;
		LeafCost += x;
		Node* temp;
		temp = new Node(x);
		q.push(temp);
	}
	Node *lchild;
	Node *rchild;
	long long w;
	for(int i=0;i<n-1;i++)
	{
		lchild = q.top();
		q.pop();
		rchild = q.top();
		q.pop();
		w = lchild->weight+rchild->weight;
		Node* temp;
		temp =  new Node(w,lchild,rchild);
		q.push(temp);
	}
	Node* root = q.top();
	InOrder(root);
	cout<<TotalCost-LeafCost<<endl;
	freeTree(root);
	return 0;
}
